<?php
	class _fo_webtraffic
	{
		var $_sum = array();
		var $_last_id = 0;
		var $_sum_id = 0;
		var $_del = false;
		var $_maj = false;
		var $_id = 0;
		var $_db;
		var $_wrapper;
		var $_pk = 'id';
		var $_count = 0;
		var $_res = array();
		var $_fields = array();
		public $id;
		public $date_track;
		public $ip;
		public $page_track;
		public $language;
		public $country;
		public $abbrevcountry;
		public $city;
		public $coordinates;
		public $os;
		public $browser;
		public $referer;
		
		static $_instance;
		
		// singleton
		public function &getInstance() 
		{
			if (!self::$_instance instanceof self)
				self::$_instance = new self();
			return self::$_instance;
		}
		
		function __construct()
		{
			$this -> _wrapper = GP_wrapper::getInstance();
			$this -> _wrapper -> settable('_fo_webtraffic');
			$this -> _db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
			$this -> _pk = $this -> _wrapper -> _pk;
			$this -> fields();
		}
		
		function fields()
		{
			$q = "DESCRIBE _fo_webtraffic";
			$res = $this -> _db -> fetchArray($q);
			foreach ($res as $f => $tab)
			{
				foreach ($tab as $type => $champ)
				{
					if ($type == 'Field')
					{
						$this -> $champ = '';
						$this -> _fields[] = $champ;
					}
				}
			}
		}
		
		function setId($val)
		{
			$this -> id = $val;
		}
		
		function getId()
		{
			return $this -> id;
		}

		function setDatetrack($val)
		{
			$this -> date_track = $val;
		}
		
		function getDatetrack()
		{
			return $this -> date_track;
		}

		function setIp($val)
		{
			$this -> ip = $val;
		}
		
		function getIp()
		{
			return $this -> ip;
		}

		function setPagetrack($val)
		{
			$this -> page_track = $val;
		}
		
		function getPagetrack()
		{
			return $this -> page_track;
		}

		function setLanguage($val)
		{
			$this -> language = $val;
		}
		
		function getLanguage()
		{
			return $this -> language;
		}

		function setCountry($val)
		{
			$this -> country = $val;
		}
		
		function getCountry()
		{
			return $this -> country;
		}

		function setAbbrevcountry($val)
		{
			$this -> abbrevcountry = $val;
		}
		
		function getAbbrevcountry()
		{
			return $this -> abbrevcountry;
		}

		function setCity($val)
		{
			$this -> city = $val;
		}
		
		function getCity()
		{
			return $this -> city;
		}

		function setCoordinates($val)
		{
			$this -> coordinates = $val;
		}
		
		function getCoordinates()
		{
			return $this -> coordinates;
		}

		function setOs($val)
		{
			$this -> os = $val;
		}
		
		function getOs()
		{
			return $this -> os;
		}

		function setBrowser($val)
		{
			$this -> browser = $val;
		}
		
		function getBrowser()
		{
			return $this -> browser;
		}

		function setReferer($val)
		{
			$this -> referer = $val;
		}
		
		function getReferer()
		{
			return $this -> referer;
		}
		
		function insert($ins)
		{
			$this -> _wrapper -> insert($ins);
			return $this -> LastId();
		}
		
		function update($maj, $condition = '')
		{
			$res = $this -> _wrapper -> update($maj, $condition);
			$this -> _maj = $res;
		}
		
		function updateid($maj, $id)
		{
			$res = $this -> _wrapper -> updateid($maj, $id);
			$this -> _id = $id;
			$this -> _maj = $res;
		}
		
		function delete($condition = '')
		{
			$res = $this -> _wrapper -> delete($condition);
			$this -> _del = $res;
		}
		
		function deleteid($id)
		{
			$res = $this -> _wrapper -> deleteid($id);
			$this -> _id = $id;
			$this -> _del = $res;
		}
		
		function count($condition = '')
		{
			$this -> _count = $this -> _wrapper -> count($condition);
		}
		
		function select($champs = '', $condition = '')
		{
			if ((strlen($champs) || count($champs)) && strlen($condition))
				$res = $this -> _wrapper -> select($champs, $condition);
			else if ((!strlen($champs) && !count($champs)) && strlen($condition))
				$res = $this -> _wrapper -> select('', $condition);
			else if ((strlen($champs) || count($champs)) && !strlen($condition))
				$res = $this -> _wrapper -> select($champs);
			$this -> _count = count($res);
			$this -> _res = $res;
		}
		
		function selectid($id, $champs = '')
		{
			if (strlen($champs) || count($champs))
				$res = $this -> _wrapper -> selectid($id, $champs);
			else
				$res = $this -> _wrapper -> selectid($id);
			
			foreach ($res as $tab)
				foreach($tab as $champ => $valeur)
					$this -> $champ = $valeur;
			$this -> _id = $id;
		}
		
		function sum($champs, $condition = '')
		{
			$res = $this -> _wrapper -> sum($champs, $condition);
			if (is_array($res))
			{
				foreach ($res as $champ => $valeur)
					$this -> $champ = $valeur;
			}
			else
				$this -> _sum = $res;
		}
		
		function sumid($champs, $id)
		{
			$this -> _id = $id;
			$res = $this -> _wrapper -> sumid($champs, $id);
			$this -> _sum_id = $res;
		}
		
		function LastId()
		{
			$this -> _id = $this -> _last_id = $this -> _wrapper -> LastId();
			return $this -> _last_id;
		}
		
		function add()
		{
			GP_ActiveRecord::setDefaultDBConnection($this -> _db);
			$ins = new GP_ActiveRecord('_fo_webtraffic');
			
			foreach ($this -> _fields as $champ)
				$ins -> $champ = $this -> $champ;
			$ins -> soumettre();
			$this -> _id = $ins -> getKey();
		}
		
		function maj()
		{
			GP_ActiveRecord::setDefaultDBConnection($this -> _db);
			$maj = new GP_ActiveRecord('_fo_webtraffic', $this -> _id);
			$tabold = $maj -> get($this -> _fields, $this -> _pk . " = '" . $this -> _id . "'");
			$old = $tabold[0];
			
			foreach ($this -> _fields as $champ)
				$maj -> $champ = (strlen($this -> $champ)) ? $this -> $champ : $old[$champ];
			$maj -> soumettre();
		}
		
		function save()
		{
			if (isset($this -> _id))
				$this -> maj();
			else
				$this -> add();
		}
		
		function get()
		{
			if (strlen($this -> _id) > 0)
			{
				GP_ActiveRecord::setDefaultDBConnection($this -> _db);
				$get = new GP_ActiveRecord('_fo_webtraffic', $this -> _id);
				$tab = $get -> get($this -> _fields, $this -> _pk . " = '" . $this -> _id . "'");
				$data = $tab[0];
				foreach ($this -> _fields as $champ)
					$this -> $champ = $data[$champ];
			}
		}
	}
?>